package com.zhiche.lisa.lspm.service.weiXin.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.lspm.dao.mapper.weiXin.WeiXinExportMapper;
import com.zhiche.lisa.lspm.dao.model.weiXin.WeiXinExport;
import com.zhiche.lisa.lspm.service.utils.ErpUtil;
import com.zhiche.lisa.lspm.service.weiXin.WeiXinExportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 微信使用状况导出 服务实现类
 * </p>
 *
 * @author zhangkun
 * @since 2018-10-24
 */
@Service
public class WeiXinExportServiceImpl extends ServiceImpl<WeiXinExportMapper, WeiXinExport> implements WeiXinExportService {
    private static final Logger LOGGER = LoggerFactory.getLogger(WeiXinExportServiceImpl.class);
    @Autowired
    private ErpUtil erpUtil;

    /**
     * 查询微信公众号使用率分页返回
     *
     * @param page
     * @return
     */
    @Override
    public Page<WeiXinExport> queryWeChatPublicNumberUsageRate(Page<WeiXinExport> page) {
        if (page == null) {
            throw new BaseException("分页参数不为空");
        }
        Map<String, Object> cn = page.getCondition();
        Wrapper<WeiXinExport> ew = buildCondition(cn);
        List<WeiXinExport> weiXinExportList = baseMapper.selectPage(page, ew);
        for (int i = 0; i < weiXinExportList.size(); i++) {
            WeiXinExport weiXinExport1 = weiXinExportList.get(i);
            double rate = weiXinExport1.getQueueCount().doubleValue() / weiXinExport1.getShipCount().doubleValue();
            weiXinExport1.setRate(rate);
            Date date = new Date();
            String lastTimeStamp = String.valueOf(date.getTime());
            weiXinExport1.setLastTimeStamp(lastTimeStamp);
        }
        page.setRecords(weiXinExportList);
        return page;
    }

    /**
     * 获取微信公众号使用率情况
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertERPRate() {
        String lastTimeStamp;
        EntityWrapper<WeiXinExport> ew = new EntityWrapper<>();
        ew.orderBy("id", false);
        WeiXinExport weiXinExport = this.selectOne(ew);
        if (Objects.nonNull(weiXinExport)) {
            lastTimeStamp = weiXinExport.getLastTimeStamp();
        } else {
            lastTimeStamp = "";
        }
        List<WeiXinExport> weiXinExportList = erpUtil.getWeChatPublicNumberUsageRate(lastTimeStamp);
        if (!CollectionUtils.isEmpty(weiXinExportList)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -1);
            long yesTime = calendar.getTime().getTime();
            for (WeiXinExport weiXinExport1 : weiXinExportList) {
                weiXinExport1.setLastTimeStamp(String.valueOf(yesTime));
                weiXinExport1.setStatus("10");
            }
            this.insertBatch(weiXinExportList, 100);
        }
    }

    /**
     * 查询微信公众号使用率全部返回
     */
    @Override
    public List<WeiXinExport> queryWeChatPublicNumberUsageRate(Map<String, Object> conditon) {
        Wrapper<WeiXinExport> ew = buildCondition(conditon);
        List<WeiXinExport> weiXinExportList = this.selectList(ew);
        for (WeiXinExport weiXinExport1 : weiXinExportList) {
            double rate = weiXinExport1.getQueueCount().doubleValue() / weiXinExport1.getShipCount().doubleValue();
            weiXinExport1.setRate(rate);
        }
        return weiXinExportList;
    }

    private Wrapper<WeiXinExport> buildCondition(Map<String, Object> condition) {
        Wrapper<WeiXinExport> ew = new EntityWrapper<>();
        if (!CollectionUtils.isEmpty(condition)) {
            //根据调度时间查询
            Object dtship = condition.get("dtship");
            Object dtship2 = condition.get("dtship2");
            if (!StringUtils.isEmpty(dtship)) {
                ew.ge("dtship", dtship);
            }
            if (!StringUtils.isEmpty(dtship2)) {
                ew.le("dtship", dtship2);
            }
            //根据启运地查询
            Object origin = condition.get("origin");
            if (!StringUtils.isEmpty(origin.toString())) {
                ew.like("origin", origin.toString());
            }
        }
        ew.orderBy("dtship desc, id desc");
        return ew;
    }

}
